import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
 * GameUI or game graphic interface that will be the main part of the program that will run the game
 * @author Kridpak Vorametpasuk 5510546638
 * @version May 09 2013
 */
public class GameUI extends JFrame {

	public JButton[] buttons;
	public JLabel questionNum, timer, score, quesText;
	public JPanel panelLeft, panelRight;
	public JPanel[][] panelAll;
	public int scorePoint;
	public int time, quesCou, quest;
	private GameFactory game;
	private ArrayList<Integer> random;
	private RetryUI retry;
	private int sum;
	public int count;
	private Timer timermer;

	/**
	 * Constructure that will reset all the varible's value
	 * @param game is the game form
	 */
	public GameUI(GameFactory game) {
		this.game = game;
		time = 60;
		sum = 0;
		retry = new RetryUI(scorePoint);

	}

	/**
	 * setScore to other class
	 * @return scorePoint or that score that player got at that time
	 */
	public int getScore() {
		return scorePoint;
	}

	/**
	 * create Game interface and set up each button for the game
	 */
	public void initComponent() {

		timermer = new Timer(1000, new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				time--;
				timer.setText("Time: " + time);
				System.out.println(time);
				if (time % 3 == 0) {
					scorePoint++;
					score.setText("Score: " + scorePoint);
				}

				if (time == 0) {
					retry = new RetryUI(scorePoint);
					retry.run();
					dispose();
					timermer.stop();
				}
			}

		});

		timermer.start();

		panelAll = new JPanel[4][6];
		setLayout(new GridLayout(4, 6, 0, 0));

		for (int m = 0; m < 4; m++) {
			for (int n = 0; n < 6; n++) {
				panelAll[m][n] = new JPanel();
				add(panelAll[m][n]);
			}
		}

		timer = new JLabel("Time: " + time);
		timer.setFont(new Font("San serif", Font.BOLD, 16));
		panelAll[0][0].add(timer, BorderLayout.CENTER);

		score = new JLabel("Score: " + scorePoint);
		score.setFont(new Font("San serif", Font.BOLD, 16));
		panelAll[0][5].add(score, BorderLayout.CENTER);

		quesCou = 1;
		quesText = new JLabel("Ques No." + quesCou);
		quesText.setFont(new Font("San serif", Font.BOLD, 24));
		panelAll[1][0].add(quesText);

		questionNum = new JLabel("" + quest);
		questionNum.setFont(new Font("Time new Roman", Font.ITALIC, 48));
		panelAll[2][0].add(questionNum);

		buttons = new JButton[9];
		for (int k = 0; k < buttons.length; k++) {
			buttons[k] = new JButton();
			buttons[k].setText(random.get(k).toString());
			buttons[k].setPreferredSize(new Dimension(85, 50));
			buttons[k].addActionListener(new ButtonListener());
		}

		panelAll[1][2].add(buttons[0], BorderLayout.WEST);
		panelAll[1][3].add(buttons[1], BorderLayout.WEST);
		panelAll[1][4].add(buttons[2], BorderLayout.WEST);
		panelAll[2][2].add(buttons[3], BorderLayout.WEST);
		panelAll[2][3].add(buttons[4], BorderLayout.WEST);
		panelAll[2][4].add(buttons[5], BorderLayout.WEST);
		panelAll[3][2].add(buttons[6], BorderLayout.WEST);
		panelAll[3][3].add(buttons[7], BorderLayout.WEST);
		panelAll[3][4].add(buttons[8], BorderLayout.WEST);

	}

	/**
	 * change to next question every time the player complete the question
	 */
	public void newGame() {
		game.newQuestion();
		score.setText("Score: " + scorePoint);
		quesText.setText("Ques No." + quesCou);
		shuffle(game.getNum());
		questionNum.setText("" + game.getQuestion());
		for (int k = 0; k < buttons.length; k++) {
			buttons[k].setText(random.get(k).toString());
		}
	}

	/**
	 * shuffle the number for the game 
	 * @param nums is all the numbers that will put in buttons
	 * @return new array that contain numbers that will be in game
	 */
	public List<Integer> shuffle(int[] nums) {
		quest = game.getQuestion();
		game.createNum();
		nums = game.getNum();
		random = new ArrayList<Integer>();
		for (int i = 0; i < 9; i++) {
			random.add(nums[i]);
		}
		Collections.shuffle(random);
		return random;
	}


	/**
	 * run is the method that will call the program to create the fame and set up the frame.
	 */
	public void run() {
		GameUI first = new GameUI(game);
		first.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		first.setTitle("Think it Up!!");
		game.newQuestion();
		int[] num = game.getNum();
		first.shuffle(num);
		first.setSize(730, 250);
		first.initComponent();
		first.setVisible(true);
	}

	/**
	 * class for checking buttons that has pressed
	 * 
	 */
	class ButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			JButton temp = ((JButton) arg0.getSource());

			sum += Integer.parseInt(temp.getText());
			temp.setEnabled(false);
			count++;
			if (count == 3)
				if (sum == game.getQuestion()) {
					sum = 0;
					count = 0;
					for (int k = 0; k < buttons.length; k++) {
						buttons[k].setEnabled(true);
					}
					scorePoint += (1 + quesCou) * game.getScoreExtra();
					quesCou++;
					newGame();
				} else {
					count = 0;
					timermer.stop();
					retry = new RetryUI(scorePoint);
					retry.run();
					dispose();
				}

		}

	}
}
